<?
include("../functions/functions.php");

if (isset($_POST['Year'])) {
	$thisYear = $_POST['Year']	;
}
else {
	$thisYear = date("Y");
}

$lastYear = $thisYear-1;
$reportTitle = "First visit by month for the financial year of $lastYear/$thisYear.";

$total = 0;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><? echo $reportTitle; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="reports.css" rel="stylesheet" type="text/css">
</head>

<body bgcolor="#FFFFFF" text="#000000" link="#0075C5" vlink="#0075C5" alink="#C5DDF4" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<?
echo "<p style=\"text-align:center; font-size: 16pt\">$reportTitle</h3>";
?>
<table width="100%" border="0">
	<tr>
		<td width="50%">
			<form method="post">
				Select the financial year to report on: <select name="Year" onChange="submit();">
					<?
					$years = getBeginEndYears();
					for ($j = $years[start]; $j<$years[end]; $j++) {
						$option = ($j-1)." - ".$j;
						if ($thisYear == $j)
							$select = "SELECTED";
						else
							$select = "";
						echo "<option value=\"$j\" $select>$option</option>";
					}
					?>
				</select>
			</form>
		</td>
		<td align="right" width="50%"><? echo getCurrentDate(); ?></td>
	</tr>
</table>
<?

echo "<p>First visit in a financial year per client</p>";

echo "<table width=\"100%\" cellpadding=\"3\" cellspacing=\"0\" border=\"1\" bordercolor=\"#000000\" class=\"body\" FRAME=BOX RULES=GROUPS>";

echo "<tbody><tr>
				<td class=\"bodybold\" style=\"width:50%;\">Month</td>
				<td class=\"bodybold\" style=\"width:50%;\">Total</td>
			</tr></tbody>";

$sql = "select count(a.client_id) as count_client, month(a.first_visit) as month_first_visit from
		  (
		  select a.client_id, min(a.visit_date) as first_visit from er_visit as a group by client_id
		  ) as a
		where a.first_visit > '$lastYear-07-01' and a.first_visit < '$thisYear-06-30'
		group by month_first_visit
		order by 2";
//echo $sql;

$results = r_db($sql);

while ($row = mysql_fetch_array($results)) {
	$tempCount_client = $row["count_client"];
	$tempMonth = $row["month_first_visit"];

	$currentMonth = date("F",mktime(0,0,0,$tempMonth,1,$thisYear));

	echo "<tr><td>$currentMonth</td><td>$tempCount_client</td></tr></tbody>";
	$total += $tempCount_client;
}

	echo "<tr><td class=\"bodybold\">Total new clients this financial year</td><td class=\"bodybold\">$total</td></tr></tbody>";

?>
</table>
</body>
</html>
